import 'dart:convert';
import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncfusion_flutter_pdf/pdf.dart';

part './pdf_state.dart';

class PdfCubit extends Cubit<PdfState> {
  PdfCubit() : super(PdfState());

  readPdf() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles();
    PdfDocument document;
    if (result != null) {
      if (result.files.single.bytes == null) {
        document = PdfDocument(inputBytes: File(result.files.single.path!).readAsBytesSync());
      } else {
        document = PdfDocument(inputBytes: result.files.single.bytes);
      }
      var text = PdfTextExtractor(document).extractText(startPageIndex: 0);
      emit(state.clone(pdfText: text));
      document.dispose();
    } else {
      // User canceled the picker
    }
  }
}
